//
//  SJBaseNavigationC.swift
//  SJBaseNavigationC
//
//  Created by swordjoy on 2017/4/28.
//  Copyright © 2017年 swordjoy. All rights reserved.
//

import UIKit

class SJBaseNavigationC: RTRootNavigationController {

  override var preferredStatusBarStyle: UIStatusBarStyle {
    return topViewController?.preferredStatusBarStyle ?? .default
  }
  
  override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
    return .fade
  }
  
  override func viewDidLoad() {
    super.viewDidLoad()
    
    view.backgroundColor = UIColor.white
    navigationBar.titleTextAttributes = GlobalConfig.NavBarRootTitleAttributes
    navigationBar.setBackgroundImage(GlobalConfig.BGSkyBlueImage, for: .default)
    navigationBar.shadowImage = UIImage()
  }
  
  override func pushViewController(_ viewController: UIViewController, animated: Bool) {
    
    if viewControllers.count > 0 {
      viewController.hidesBottomBarWhenPushed = true
    }
    
    if 1 == self.viewControllers.count {
      navigationBar.setBackgroundImage(GlobalConfig.BGWhiteImage, for: .default)
      navigationBar.titleTextAttributes = GlobalConfig.NavBarStackTitleAttributes
    }
    
    super.pushViewController(viewController, animated: true)
    
  }
  
  override func popViewController(animated: Bool) -> UIViewController? {
    
    if 2 == self.viewControllers.count {
      navigationBar.setBackgroundImage(GlobalConfig.BGSkyBlueImage, for: .default)
      navigationBar.titleTextAttributes = GlobalConfig.NavBarRootTitleAttributes
    }
    
    isToolbarHidden = true
    return super.popViewController(animated: animated)
  }

  override func popToRootViewController(animated: Bool) -> [UIViewController]? {
    
    navigationBar.setBackgroundImage(GlobalConfig.BGSkyBlueImage, for: .default)
    navigationBar.titleTextAttributes = GlobalConfig.NavBarRootTitleAttributes
    return super.popToRootViewController(animated: animated)
    
  }

}
